unit scCode;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBGridEh, DB, ZDataset, ComCtrls, sTreeView, ToolWin, sToolBar, ExtCtrls, 
  sStatusBar;

type
  TfmCode = class(TForm)
    dsCode: TDataSource;
    tvCat: TsTreeView;
    tbCode: TsToolBar;
    btnAddCode: TToolButton;
    btnEditeCode: TToolButton;
    btnDeleteCode: TToolButton;
    btn1: TToolButton;
    btnAddChild: TToolButton;
    stsbrTree: TsStatusBar;
    btnCollapse: TToolButton;
    btnExpand: TToolButton;
    btnSep: TToolButton;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnAddCodeClick(Sender: TObject);
    procedure btnEditeCodeClick(Sender: TObject);
    procedure btnAddChildClick(Sender: TObject);
    procedure btnDeleteCodeClick(Sender: TObject);
    procedure tvCatChange(Sender: TObject; Node: TTreeNode);
    procedure btnCollapseClick(Sender: TObject);
    procedure btnExpandClick(Sender: TObject);
    procedure tvCatDeletion(Sender: TObject; Node: TTreeNode);
  private
    procedure createTree;
    procedure deleteNode;
    procedure showCodeItem(isNew : Boolean; isChild : Boolean = False);
  public

  end;

implementation

uses scDmDao, scCodeItem, scMain, dbMyUtils, scTreeHandler;

{$R *.dfm}

procedure TfmCode.FormShow(Sender: TObject);
begin
  createTree;
end;

procedure TfmCode.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfmCode.showCodeItem(isNew : Boolean; isChild : Boolean = False);
var
  fm : TfmCodeItem;
begin
   if (not isNew) and (not Assigned(tvCat.Selected)) then Abort;
   try
     fm := TfmCodeItem.Create(Application);
     if (isNew) then
      dsCode.DataSet.Append
     else
      if not TZQuery(dsCode.DataSet).Locate('id',TNodeData(tvCat.Selected.Data).id,[]) then
        Abort;
     fm.dsCode.DataSet := dsCode.DataSet;
     fm.dsCodeParentSelect.DataSet := dmDao.getCode(fm);

     if isNew then
     begin
       if  isChild and Assigned(tvCat.Selected) then
           fm.cbbCodeParentSelect.KeyValue := TNodeData(tvCat.Selected.Data).id;
     end
     else
     begin
       if  Assigned(tvCat.Selected) and Assigned(tvCat.Selected.Parent) then
           fm.cbbCodeParentSelect.KeyValue := TNodeData(tvCat.Selected.Parent.Data).id;
     end;

     if fm.ShowModal = mrOK then
     begin
      dmDao.processNodeHref(isNew, TZQuery(dsCode.DataSet).FieldByName('id').AsInteger,
                      fm.cbbCodeParentSelect.KeyValue);
      createTree;
      getNodeById(TZQuery(dsCode.DataSet).FieldByName('id').AsInteger, tvCat).Selected := True;
     end
     else
       TZQuery(dsCode.DataSet).Cancel;
   finally
     FreeAndNil(fm);
   end;
end;

procedure TfmCode.btnAddCodeClick(Sender: TObject);
begin
   showCodeItem(True);
end;

procedure TfmCode.createTree;
begin
  tvCat.Items.Clear;
  fillTree(nil,0,tvCat);
end;       

procedure TfmCode.btnEditeCodeClick(Sender: TObject);
begin
   showCodeItem(False);
end;

procedure TfmCode.btnAddChildClick(Sender: TObject);
begin
   showCodeItem(True,True);
end;

procedure TfmCode.btnDeleteCodeClick(Sender: TObject);
begin
  deleteNode;
end;

procedure TfmCode.deleteNode;
begin
    if Assigned(tvCat.Selected) and Assigned(tvCat.Selected.Data) then
      if (TZQuery(dsCode.DataSet).Locate('id', TNodeData(tvCat.Selected.Data).id, []))
        and (dmDao.getChildNodeCount(TNodeData(tvCat.Selected.Data).id) = 0 ) then
        if DeleteCurR(dsCode.DataSet) then
          createTree;
end;

procedure TfmCode.tvCatChange(Sender: TObject; Node: TTreeNode);
begin
  stsbrTree.Panels[0].Text := TNodeData(tvCat.Selected.Data).code;
  stsbrTree.Panels[1].Text := TNodeData(tvCat.Selected.Data).name;
end;

procedure TfmCode.btnCollapseClick(Sender: TObject);
begin
  tvCat.FullCollapse;
end;

procedure TfmCode.btnExpandClick(Sender: TObject);
begin
  tvCat.FullExpand;
end;

procedure TfmCode.tvCatDeletion(Sender: TObject; Node: TTreeNode);
begin
  if Assigned(Node.Data) then
  begin
    TNodeData(Node.Data).Free;
    Node.Data := nil;
  end;
end;

end.
